package com.gb.demand.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gb.demand.common.api.ResultMap;
import com.gb.demand.common.entity.Apilog;
import com.gb.demand.common.entity.SpClientBillingapply;
import com.gb.demand.common.mapper.SpClientBillingapplyMapper;
import com.gb.demand.service.IApilogService;
import com.gb.demand.service.ISpClientBillingapplyService;
import com.gb.demand.service.IStoreService;
import com.gb.demand.service.ex.ServiceException;
import com.gb.demand.util.JsonUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户业务层模块<p>
 *
 * @author wengjiangle
 * @since 2020/7/13
 */
@Slf4j
@Service
public class SpClientBillingapplyServiceImpl extends ServiceImpl<SpClientBillingapplyMapper, SpClientBillingapply> implements ISpClientBillingapplyService {

    @Autowired
    private IStoreService storeService;

    @Autowired
    private IApilogService apilogService;

    @Resource
    private SpClientBillingapplyMapper spClientBillingapplyMapper;

    /**
     * 发票查询
     *
     * @param data
     * @return
     */
    @Override
    public Map getInvoiceInfo(String data) {
        Map fapiao = new HashMap();
        Map appinfoMap = JsonUtils.str2obj(data, Map.class);//寄送字符串转成map
        String appKey = (String) appinfoMap.get("appKey");
        String accessToken = (String) appinfoMap.get("accessToken");
        String tradeNo = (String) appinfoMap.get("tradeNo");
        // 判断token是否过期
        boolean isExpired = true;
        try {
            isExpired = storeService.checkAccessToken(appKey, accessToken);
        } catch (ServiceException e) {
            if (e.getMessage().equals("没找到该appKey所对应的店铺")) {
                // 日志
                addApiLog(appKey, data,
                        JsonUtils.obj2string(ResultMap.generateResultMapforCreateOrderForm(null, null, "ORDER014", "会员账号有误，请联系专属客户经理！", false)),
                        "新点-创建订单接口");
                return ResultMap.generateResultMapforCreateOrderForm(null, null, "ORDER014", "会员账号有误，请联系专属客户经理！", false);
            } else {
                // 日志
                addApiLog(appKey, data,
                        JsonUtils.obj2string(ResultMap.generateResultMapforCreateOrderForm(null, null, "ORDER005", e.getMessage(), false)),
                        "新点-创建订单接口");
                return ResultMap.generateResultMapforCreateOrderForm(null, null, "ORDER005", e.getMessage(), false);
            }
        } catch (Exception e) {
            // 日志
            addApiLog(appKey, data,
                    JsonUtils.obj2string(ResultMap.generateResultMapforCreateOrderForm(null, null, "ORDER008_02", "系统异常，请稍候重新尝试", false)),
                    "新点-创建订单接口");
            return ResultMap.generateResultMapforCreateOrderForm(null, null, "ORDER008_02", "系统异常，请稍候重新尝试", false);
        }
        SpClientBillingapply clientBillingapply = new SpClientBillingapply();
        if (isExpired) {
            try {
                SpClientBillingapply apply = spClientBillingapplyMapper.selTradeNo(tradeNo);
                fapiao.put("invoiceType", apply.getInvoiceType());
                fapiao.put("invoiceNo", apply.getInvoiceNo());
                fapiao.put("invoiceMoney", apply.getTotalprice());
                return ResultMap.generateResultMapwithKey("invoiceInfo", fapiao, "发票查询成功", true);
            } catch (Exception e) {
                return ResultMap.generateResultMapwithKey("invoiceInfo", "", e.getMessage(), false);
            }
        } else {
            return ResultMap.generateResultMapwithKey("invoiceInfo", "", "token已经过期了", false);
        }
    }

        /**
         * 日志记录
         *
         * @param caller      调用者
         * @param inputvalue  输入参数
         * @param outputvalue 输出参数
         * @param remark      备注
         */

    public void addApiLog(String caller, String inputvalue, String outputvalue, String remark) {
        // 记录调用日志
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Apilog apilog = new Apilog(new Date(), false, "", request.getRequestURL().toString(), caller, inputvalue, outputvalue, remark);
        apilogService.insertApilog(apilog);
    }
}
